using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using Entities;

namespace DAL
{
 public    class tbTrangthaidonhangDAL
    {
      private SqlConnection cn = null;
       public tbTrangthaidonhangDAL()
       {
           cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
           cn.Open();
          
       }
       public void Insert(tbTrangthaidonhang  ttdh)
       {
           SqlCommand cm = new SqlCommand("Trangthai_hdon_Insert", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Trangthai", ttdh.Trangthai  );
           cm.ExecuteNonQuery();
       }
       public void Update(tbTrangthaidonhang  ttdh)
       {
           SqlCommand cm = new SqlCommand("Trangthai_hdon_Update", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Matinhtrangdh", ttdh.Matrangthaidh );
           cm.Parameters.AddWithValue("@Trangthai", ttdh.Trangthai );
           cm.ExecuteNonQuery();
       }
       public void Delete(int iD)
       {
           SqlCommand cm = new SqlCommand("Trangthai_hdon_Delete", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Matinhtrangdh", iD );
           cm.ExecuteNonQuery();
       }
       public tbTrangthaidonhangCollection  Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
       {
           SqlCommand cm = new SqlCommand("Trangthai_hdon_Get", cn);
          cm.CommandType = CommandType.StoredProcedure;
          cm.Parameters.AddWithValue("@Where", where);
          cm.Parameters.AddWithValue("@PageIndex", pageIndex);
          cm.Parameters.AddWithValue("@PageSize", pageSize);
          cm.Parameters.AddWithValue("@OrderBy", orderBy);
          cm.Parameters.AddWithValue("@OrderDirection",orderDirection);
          cm.Parameters.Add(new SqlParameter("@TotalRecords",DbType.Int32)).Direction =ParameterDirection.Output;
          SqlDataReader dr = cm.ExecuteReader();
         tbTrangthaidonhangCollection  ttdhCollection = new tbTrangthaidonhangCollection  ();
         while (dr.Read())
         {
             tbTrangthaidonhang  ttdh= new tbTrangthaidonhang  ();
          ttdh .Matrangthaidh  = int.Parse(dr.GetValue(0).ToString());
             ttdh.Trangthai  = dr.GetValue(1).ToString();
             ttdhCollection.Add(ttdh);
         }
         dr.Close();
         totalRecords = int.Parse(
         cm.Parameters["@TotalRecords"].Value.ToString());
         return ttdhCollection ;
       }
    }
}
